package com.googlecode.linkmein.service;

import com.googlecode.linkmein.domain.Employee;
import com.googlecode.linkmein.domain.Employees;
import com.googlecode.linkmein.domain.Project;
import com.googlecode.linkmein.domain.Projects;
import com.googlecode.linkmein.domain.revision.*;
import com.googlecode.linkmein.repository.ObjectStorage;

import java.util.ArrayList;
import java.util.Date;

public class CompareRevisionService {
    private ObjectStorage storage;

    public CompareRevisionService(ObjectStorage storage) {
        this.storage = storage;
    }

    public void compareAndStoreEmployeesRevision(Employees employees, Employees newEmployees) {
        ArrayList<Change> changes = new ArrayList<Change>();
        for (Employee employee : newEmployees) {
            if (!employees.contains(employee)) {
                NewEmployeeChange newEmployeeChange = new NewEmployeeChange();
                newEmployeeChange.setEmployee(employee);
                newEmployeeChange.setCreatedAt(new Date());
                changes.add(newEmployeeChange);
            }
        }

        for (Employee employee : employees) {
            if (!newEmployees.contains(employee)) {
                EmployeeQuitChange employeeQuitChange = new EmployeeQuitChange();
                employeeQuitChange.setEmployee(employee);
                employeeQuitChange.setCreatedAt(new Date());
                changes.add(employeeQuitChange);
            }
        }
        storage.store(changes);
    }

    public void compareAndStoreProjectsRevision(Projects projects, Projects newProjects) {
        ArrayList<Change> changes = new ArrayList<Change>();
        for (Project project : newProjects) {
            if (!projects.contains(project)) {
                NewProjectChange newProjectChange = new NewProjectChange();
                newProjectChange.setProject(project);
                newProjectChange.setCreatedAt(new Date());
                changes.add(newProjectChange);
            }
        }

        for (Project project : projects) {
            if (!newProjects.contains(project)) {
                ProjectEndChange projectEndChange = new ProjectEndChange();
                projectEndChange.setProject(project);
                projectEndChange.setCreatedAt(new Date());
                changes.add(projectEndChange);
            }
        }
        storage.store(changes);
    }

}